<template>
  <div style="padding: 10px">
    <div v-for="(t, i) in list" :key="i" class="table-item">
      <h3 v-if="t.name" v-cloak>-------{{ t.name }}--------</h3>
      <el-table
        :data="t.data"
        border
        fit
        :show-summary="true"
        :summary-method="summaryMethod"
        :header-cell-style="{ backgroundColor: '#e1e1e1', color: '#000' }"
        style="width: 100%"
      >
        <el-table-column
          prop="summary.groupName"
          label="用户昵称"
        ></el-table-column>
        <el-table-column
          prop="summary.alternateName"
          label="昵称"
        ></el-table-column>
        <el-table-column
          prop="summary.currencyCode"
          label="货币"
        ></el-table-column>
        <el-table-column label="交易额">
          <el-table-column
            prop="summary.effectiveTurnOver"
            label="实"
          ></el-table-column>
          <el-table-column
            prop="summary.uplineTurnOver"
            label="上线"
          ></el-table-column>
          <el-table-column
            prop="summary.turnOver"
            label="总计"
          ></el-table-column>
        </el-table-column>
        <el-table-column
          prop="summary.incentivePaid"
          label="总佣金"
        ></el-table-column>
        <el-table-column prop="summary.payout" label="赢额"></el-table-column>
        <el-table-column label="总代理">
          <el-table-column
            prop="downLines.winLoss"
            label="盈亏"
          ></el-table-column>
          <el-table-column
            prop="downLines.grossComm"
            label="佣金"
          ></el-table-column>
          <el-table-column
            prop="downLines.grossProfit"
            label="总计"
          ></el-table-column>
        </el-table-column>
        <el-table-column prop="company" label="公司"></el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>

export default {
  props: {
    list: {
      type: Array,
      default: () => {
        return [
          {
            data: [
              {
                summary: {
                  groupKey: 424155,
                  groupName: 'XXHKTLT07657',
                  greatWall: false,
                  alternateName: null,
                  turnOver: 22739.88,
                  incentivePaid: 0,
                  effectiveTurnOver: 18191.9,
                  uplineTurnOver: 22739.89,
                  currencyCode: 'HKD',
                  levelId: 40,
                  totalWager: null,
                  turnOverInFCurr: 22739.88,
                  incentivePaidInFCurr: 0,
                  submit: 4547.99,
                  collect: 22739.89,
                  stockSubmit: null,
                  stockCollect: null,
                  stockTotal: null,
                  payout: -18098.28,
                  payoutInFCurr: -18098.28
                },
                downLines: {
                  40: {
                    groupKey: 424155,
                    levelId: 40,
                    winLoss: 14478.62,
                    grossComm: 0,
                    grossProfit: 14478.62,
                    winLossInFCurr: 14478.62,
                    grossCommInFCurr: 0,
                    grossProfitInFCurr: 14478.62
                  }
                },
                company: 3619.66,
                companyInFCurr: 3619.66,
                groupName: 'XXHKTLT07657',
                groupKey: 424155
              },
              {
                summary: {
                  groupKey: 424172,
                  groupName: 'XXHKTLT07672',
                  greatWall: false,
                  alternateName: null,
                  turnOver: 224813.08,
                  incentivePaid: 0,
                  effectiveTurnOver: 179850.47,
                  uplineTurnOver: 224813.3,
                  currencyCode: 'HKD',
                  levelId: 40,
                  totalWager: null,
                  turnOverInFCurr: 224813.08,
                  incentivePaidInFCurr: 0,
                  submit: 44962.83,
                  collect: 224813.3,
                  stockSubmit: null,
                  stockCollect: null,
                  stockTotal: null,
                  payout: 12468.92,
                  payoutInFCurr: 12468.92
                },
                downLines: {
                  40: {
                    groupKey: 424172,
                    levelId: 40,
                    winLoss: -9975.13,
                    grossComm: 0,
                    grossProfit: -9975.13,
                    winLossInFCurr: -9975.13,
                    grossCommInFCurr: 0,
                    grossProfitInFCurr: -9975.13
                  }
                },
                company: -2493.79,
                companyInFCurr: -2493.79,
                groupName: 'XXHKTLT07672',
                groupKey: 424172
              },
              {
                summary: {
                  groupKey: 450881,
                  groupName: 'XXHKTLT0769H',
                  greatWall: false,
                  alternateName: 'chen999.',
                  turnOver: 230303.5,
                  incentivePaid: 0,
                  effectiveTurnOver: 184242.8,
                  uplineTurnOver: 230303.53,
                  currencyCode: 'HKD',
                  levelId: 40,
                  totalWager: null,
                  turnOverInFCurr: 230303.5,
                  incentivePaidInFCurr: 0,
                  submit: 46060.73,
                  collect: 230303.53,
                  stockSubmit: null,
                  stockCollect: null,
                  stockTotal: null,
                  payout: -96789.54,
                  payoutInFCurr: -96789.54
                },
                downLines: {
                  40: {
                    groupKey: 450881,
                    levelId: 40,
                    winLoss: 77431.63,
                    grossComm: 0,
                    grossProfit: 77431.63,
                    winLossInFCurr: 77431.63,
                    grossCommInFCurr: 0,
                    grossProfitInFCurr: 77431.63
                  }
                },
                company: 19357.91,
                companyInFCurr: 19357.91,
                groupName: 'XXHKTLT0769H',
                groupKey: 450881
              },
              {
                summary: {
                  groupKey: 449906,
                  groupName: 'XXHKTLT0769T',
                  greatWall: false,
                  alternateName: null,
                  turnOver: 8533.5,
                  incentivePaid: 0,
                  effectiveTurnOver: 6826.8,
                  uplineTurnOver: 8533.67,
                  currencyCode: 'HKD',
                  levelId: 40,
                  totalWager: null,
                  turnOverInFCurr: 8533.5,
                  incentivePaidInFCurr: 0,
                  submit: 1706.87,
                  collect: 8533.67,
                  stockSubmit: null,
                  stockCollect: null,
                  stockTotal: null,
                  payout: -597.75,
                  payoutInFCurr: -597.75
                },
                downLines: {
                  40: {
                    groupKey: 449906,
                    levelId: 40,
                    winLoss: 478.2,
                    grossComm: 0,
                    grossProfit: 478.2,
                    winLossInFCurr: 478.2,
                    grossCommInFCurr: 0,
                    grossProfitInFCurr: 478.2
                  }
                },
                company: 119.55,
                companyInFCurr: 119.55,
                groupName: 'XXHKTLT0769T',
                groupKey: 449906
              },
              {
                summary: {
                  groupKey: 444771,
                  groupName: 'XXHKTLT076CB',
                  greatWall: false,
                  alternateName: 'chen888.',
                  turnOver: 2107930,
                  incentivePaid: 0,
                  effectiveTurnOver: 1686344,
                  uplineTurnOver: 2107930,
                  currencyCode: 'HKD',
                  levelId: 40,
                  totalWager: null,
                  turnOverInFCurr: 2107930,
                  incentivePaidInFCurr: 0,
                  submit: 421586,
                  collect: 2107930,
                  stockSubmit: null,
                  stockCollect: null,
                  stockTotal: null,
                  payout: -396928.2,
                  payoutInFCurr: -396928.2
                },
                downLines: {
                  40: {
                    groupKey: 444771,
                    levelId: 40,
                    winLoss: 317542.56,
                    grossComm: 0,
                    grossProfit: 317542.56,
                    winLossInFCurr: 317542.56,
                    grossCommInFCurr: 0,
                    grossProfitInFCurr: 317542.56
                  }
                },
                company: 79385.64,
                companyInFCurr: 79385.64,
                groupName: 'XXHKTLT076CB',
                groupKey: 444771
              }
            ]
          }
        ]
      }
    }
  },
  data () {
    return {}
  },
  methods: {
    formatMoney (val, w = 2) {
      // 金额转换 分->元 保留2位小数 并每隔3位用逗号分开 1,234.56
      const str = (val / 1).toFixed(w) + ''
      const intSum = str.substring(0, str.indexOf('.')).replace(/\\B(?=(?:\\d{3})+$)/g, ',')// 取到整数部分
      const dot = str.substring(str.length, str.indexOf('.'))// 取到小数部分搜索
      const ret = intSum + dot
      return ret
    },
    summaryMethod (param) {
      const { columns, data } = param
      console.log(columns)
      console.log(data)
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '汇总'
          return
        }
        const values = data.map(item => Number(item[column.property]))
        // const values = data.map(item => Number(getObjData(item, column.property)))
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] += ''
        } else {
          sums[index] = 'N/A'
        }
      })

      return sums
    }
  }
}

</script>

<style>
.table-row-first:hover > td {
  background-color: #7392a6 !important;
  color: #fff;
}
</style>
